<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="dialogVisible = true">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="code" label="标识"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="bian(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="getPermissionDetail(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新增权限点" :visible.sync="dialogVisible" @close="btnCancel">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" style="width: 80%;" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" style="width: 80%;" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" style="width: 80%;" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="form.enVisible" active-value="1" inactive-value="0" size="mini" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="9">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, deletePermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        pid: 0,
        type: 1,
      },
      codeId: 0,
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0)
    },
    btnCancel() {
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        pid: 0,
        type: 1,
      }
      this.dialogVisible = false// 关闭弹层
    },
    submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            // 更新表单的逻辑
            await updatePermission({ id: this.codeId, ...this.form })
            this.fuyong()
            this.$message.success('更新角色成功')
          } else {
            await addPermission(this.form)
            this.fuyong()
            this.$message.success('新增角色成功')
          }
        }
      });
    },
    bian(id) {
      this.form.pid = id
      this.form.type = 2
      this.dialogVisible = true
    },
    fuyong() {
      this.getPermissionList()
      this.btnCancel() // 关闭弹层
    },
    deletePermission(id) {
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deletePermission(id)
        this.getPermissionList()
        this.$message.success('删除权限成功')
      })
    },
    async getPermissionDetail(id) {
      this.form = await getPermissionDetail(id)
      this.codeId = id
      this.dialogVisible = true
    }
  }
}
</script>
<style>
/* .btn-add {
  margin: 10px;
} */
.dialog-footer {
  text-align: right;
}

.container .app-container {
  padding: 20px;
  background-color: #fff;
}
</style>
